<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class CommonTeacher extends ApiHomeController {

	function __construct() { 
		parent::__construct();
		$this->load->model("Attend_model");
	}

	/**  
	* 获取老师的被听课评分(授课老师)
	* @access public 
	* @param  $template_code    模板号
	* @param  $mobile_array    手机号数组
	* @param  $verify_code_array    参数=>值 数组
	*/
	public function getTeacherScore(){
		if(!isset($_POST['teacher_code']) || $_POST['teacher_code'] == ""){
			exit(retJson('获取失败,请传入授课老师工号', false));
		}

		//获取当前年
		$year = date("Y",time()); //今年
		$last_year = $year -1; //去年
		$before_year= $year -2; //去年
		$yearlist = $this->selectTeacherSore($year,$_POST['teacher_code']);//今年的总平均分
		$last_yearlist = $this->selectTeacherSore($last_year,$_POST['teacher_code']);//去年的总平均分
		$before_yearlist = $this->selectTeacherSore($before_year,$_POST['teacher_code']);//前年的总平均分
		$data[] = $yearlist;
		$data[] = $last_yearlist;
		$data[] = $before_yearlist;
		exit(retJson('获取成功', true, $data));
	}

	//查询某个授课老师被听课的总平均分
	function selectTeacherSore($year,$teacher_code){
		//获取当前年
		$con = array(
            'where' => $this->sqlLikeEscape("teacher_code = ?? and status=??", array($teacher_code,AttendStatus::Submit[XPHEnum::Value])),
            'items' => 'academic_year,semester,class_name,teacher_name,listen_name, created, score,FROM_UNIXTIME(created) createdChar',
        );
		$con['where'] .= " and academic_year like '%-".$year."%'";
        $info = $this->Attend_model->get_list_full($con);
		$totalAverageSore = 0;
		if($info){
			$totalSore = 0;//计算总平均分
			foreach($info as $k=>$v){
				$totalSore += $v['score'];
			}
			$totalAverageSore = round($totalSore/count($info),2) ;
		}
		$data['year'] = $year-1;
		$data['totalAverageSore'] = $totalAverageSore;
		return $data;
	}

		
}